package com.buaa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buaa.common.Result;
import com.buaa.entity.User;
import com.buaa.entity.UserActivity;
import com.buaa.enums.OperationType;
import com.buaa.mapper.UserActivityMapper;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserActivityServiceImpl extends ServiceImpl<UserActivityMapper, UserActivity> implements IService<UserActivity> {

    public Result deleteByUserId (  Integer id, HttpServletRequest request){
        return null;
    }
    public Map<String,Object> findPage(String policeId, String userName, Integer pageNum, Integer pageSize) {
        // 创建一个分页对象，指定页码和每页大小
        IPage<UserActivity> page = new Page<>(pageNum, pageSize);
        QueryWrapper<UserActivity> queryWrapper = new QueryWrapper<>();
        if ((null != userName && "" != userName)  && ( null != policeId && "" != policeId )) {
            queryWrapper.like("user_name", userName);
            queryWrapper.like("police_id", policeId);
        }
        else {
            if (null != userName && "" != userName)
                queryWrapper.like("user_name", userName);
            if (null != policeId && "" != policeId)
                queryWrapper.like("police_id", policeId);
        }
        long total = count(queryWrapper);
        queryWrapper.orderByDesc("revision_time")
                .last("LIMIT " + pageNum + "," + pageSize);
        //        // 调用page方法，根据分页对象查询用户信息
        IPage<UserActivity> userPage = page(page, queryWrapper); // 调用 page 方法
        List<UserActivity> userList = userPage.getRecords();
        Map<String,Object> res=new HashMap<>();
        res.put("pagedata",userList);
        res.put("total",total);
        return res;
        // 返回查询结果中的用户记录列表
    }

    public void saveLoginoutActivity(User user) {
        // 登录成功，记录登录日志
        UserActivity userActivity = new UserActivity();
        BeanUtil.copyProperties(user, userActivity, true);
        Integer id = null;
        userActivity.setId(id); // 便于使用DB中的字段
        userActivity.setUid(user.getId()); // mapping
        userActivity.setCreateBy(user.getUserName()); // 设置操作人
        userActivity.setCreateTime(LocalDateTime.now());
        userActivity.setRevisionTime(null);
        userActivity.setReviser(null);
        /*
        userActivity.setUid(user.getId()); // mapping
        userActivity.setPoliceId(user.getPoliceId());
        userActivity.setUserName(user.getUserName());
        userActivity.setUserPassword(user.getUserPassword());
        userActivity.setCreateTime(new Date());
        userActivity.setCreateBy(user.getUserName());// 设置操作人
        userActivity.setLockStatus(user.getLockStatus());
        */
        userActivity.setOperationType(OperationType.USER_LOGOUT_OK.getDesc());

        save(userActivity); // 没有捕捉异常
    }

    public void saveLoginActivity(User user) {
        // 登录成功，记录登录日志
        UserActivity userActivity = new UserActivity();
        BeanUtil.copyProperties(user, userActivity, true);
        Integer id = null;
        userActivity.setId(id); // 便于使用DB中的字段
        userActivity.setUid(user.getId()); // mapping
        userActivity.setCreateBy(user.getUserName()); // 设置操作人
        userActivity.setCreateTime(LocalDateTime.now());
        userActivity.setRevisionTime(LocalDateTime.now());
        userActivity.setReviser(user.getUserName());
        /*
        userActivity.setUid(user.getId()); // mapping
        userActivity.setPoliceId(user.getPoliceId());
        userActivity.setUserName(user.getUserName());
        userActivity.setUserPassword(user.getUserPassword());
        userActivity.setCreateTime(new Date());
        userActivity.setCreateBy(user.getUserName());// 设置操作人
        userActivity.setLockStatus(user.getLockStatus());
        */
        userActivity.setOperationType(OperationType.USER_LOGIN_OK.getDesc());

        save(userActivity); // 没有捕捉异常
    }
}
